package com.shiwaixiangcun.customer.pay

/**
 * 支付宝支付结果封装类
 * @author Administrator
 */

class AliResult(rawResult: String) {
    lateinit var resultStatus: String
    private var result: String? = null
    private var memo: String? = null

    init {
        try {
            val resultParams = rawResult.split(";".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
            for (resultParam in resultParams) {
                if (resultParam.startsWith("resultStatus")) {
                    resultStatus = gatValue(resultParam, "resultStatus")
                }
                if (resultParam.startsWith("result")) {
                    result = gatValue(resultParam, "result")
                }
                if (resultParam.startsWith("memo")) {
                    memo = gatValue(resultParam, "memo")
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

    }

    override fun toString(): String {
        return ("resultStatus={" + resultStatus + "};memo={" + memo
                + "};result={" + result + "}")
    }

    private fun gatValue(content: String, key: String): String {
        val prefix = key + "={"
        return content.substring(content.indexOf(prefix) + prefix.length,
                content.lastIndexOf("}"))
    }
}
